<template>
	<view>
		<view class="singin" v-show="!signup">
			<button class="btn" @click="wechatSignin">微信登录</button>
		</view>
		<view class="singup" v-show="signup">
			<view class="signup-box">
				<input class="signup-input" v-model="mobile" type="text" placeholder="请输入您的手机号码" value="" />
				<input type="text" class="signup-input" v-model="nickName" placeholder="请输入您的姓名" value="" />
				<button type="primary" class="sign-btn" @click="signUp">开始注册</button>
			</view>
		</view>
	</view>
</template>

<script>
	import logic from '../../common/logic.js';
	export default {
		data() {
			return {
				openid:'',
				signup:false,
				code:'',
				mobile:'',
				nickName:'',
				deny:uni.getStorageSync('deny')
			}
		},
		methods: {
			//微信注册
			signUp(){
				let _this = this;
				if(_this.openid.length == 0){
					uni.showToast({
						title:'openId错误',
						icon:'none',
						duration:3000
					});
					return;
				}
				let reg = /^1[356789][0-9]{9}$/;
				let reg_name = /^[\u4e00-\u9fa5]{1,6}(·[\u4e00-\u9fa5]{1,6}){0,2}$/;
				console.log(_this.nickName);
				console.log(reg_name.test(_this.nickName));
				if(reg.test(_this.mobile) && reg_name.test(_this.nickName)){
					var vd = {
						mobile:_this.mobile,
						wechat:_this.openid,
						nickName:_this.nickName
					}
					console.log(vd);
					uni.request({
						url:String(logic.urls.userSignup),
						data: vd,
						method: 'POST',
						header: {
							'content-type': 'application/json'
						},
						success: (res) => {
							let rs = res.data;
							console.log(res)
							if(rs.status == 1){
								_this.signup = false;
								logic.global.isLogin = true;
								logic.userInfo.userId = rs.data.userId;
								uni.showToast({
									title:'注册成功，3秒后跳转到首页',
									icon:'none',
									duration:3000,
									success:() => {
										setTimeout(()=>{
											uni.redirectTo({
												url:'../../pages/index/index'
											});
										},3000);
									}
								});
							}else if(rs.status == 2){
								uni.showToast({
									title:'手机号' + _this.mobile + '己被注册',
									icon:'none',
									duration:3000
								});
								return;
							}else if(rs.status == 3){
								uni.showToast({
									title:'此微信号己被注册，请更换微信号后再进行注册',
									icon:'none',
									duration:3000
								});
								return;
							}else{
								uni.showToast({
									title:rs.msg,
									icon:'none',
									duration:3000
								});
								return;
							}
						},
						fail: (res) => {
							
						}
					})
				}else{
					if(!reg.test(_this.mobile)){
						uni.showToast({
							title:'手机号码格式错误，请正确输入手机号码',
							icon:'none',
							duration:3000
						});
					}else{
						uni.showToast({
						title:'姓名至少为两个汉字，请正确输入姓名',
						icon:'none',
						duration:3000
					});
					}
					return;
				}
			},
			//微信登录
			wechatSignin(){
				let _this = this;
				let atu = uni.getStorageSync('isCanUse');
				if(this.openid.length == 0){
					uni.showToast({
						title:'微信号不能为空，请刷新后再试',
						icon:'none',
						duration:3000,
						success:function(){
							setTimeout(()=>{
								uni.redirectTo({
									url:'../../pages/login/login'
								})
							},3000);
						}
					});
				}else{
					uni.request({
						url:String(logic.urls.wechatSignIn),
						data: {
							openid: _this.openid,
						},
						method: 'POST',
						header: {
							'content-type': 'application/json'
						},
						success:(res) => {
							console.log(res);
							let rs = res.data;
							if(rs.status == 1){
								logic.global.isLogin = true;
								logic.userInfo.userId = rs.data.userId;
								console.log(logic.global.isLogin);
								uni.showToast({
									title:'登录成功，1秒后跳转到首页',
									icon:'none',
									duration:1000,
									success:() => {
										setTimeout(()=>{
											uni.redirectTo({
												url:'../../pages/index/index'
											});
										},1000);
									}
								});
							}else if(rs.status == -2){
								uni.showToast({
									title:'code过期，请刷新后再试',
									icon:'none',
									duration:3000,
									success:() => {
										setTimeout(()=>{
											uni.redirectTo({
												url:'../../pages/login/login'
											});
										},3000);
										
									}
								})
							}else if(rs.status == -3){
								uni.showToast({
									title:'此微信号还未注册，请先注册后再进行登录',
									icon:'none',
									duration:5000,
									success:() => {
										setTimeout(()=>{
											_this.signup = true;
										},3000);
									}
								})
							}else{
								
							}
						},
						fail:(res) => {
							
						}
					})
				}
			},
			//获取微信信息及openId
			login() {
				let _this = this;
				uni.showLoading({
					title: '正在获取数据，请稍后...'
				});
			 
			   // 1.wx获取登录用户code
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						let code = loginRes.code;
						_this.code = code;
						console.log(_this.code);
						if (_this.isCanUse) {
							//非第一次授权获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
 　　　　　　　　　　　　　　　　　　　//获取用户信息后向调用信息更新方法
									logic.userInfo.nickName = infoRes.userInfo.nickName; //昵称
									logic.userInfo.avatarUrl = infoRes.
									userInfo.avatarUrl; //头像
									//_this.updateUserInfo();//调用更新信息方法
								}
							});
						}
						uni.hideLoading();
						console.log(code);
						console.log(logic.urls.getWX);
						//2.将用户登录code传递到后台置换用户SessionKey、OpenId等信息
						uni.request({
							url:String(logic.urls.getWX),
							data: {
								code: code,
							},
							method: 'POST',
							header: {
								'content-type': 'application/json'
							},
							success: (res) => {
								//openId、或SessionKdy存储//隐藏loading
								console.log(res);
								uni.hideLoading();
								var rs = res.data;
								if(rs.status == 1){
									_this.openid = rs.data.openid;
									logic.userInfo.openid = rs.data.openid;
									console.log(_this.openid);
									console.log(rs.data);
									logic.userInfo.openid = rs.data.openid;
									logic.session_key = rs.session_key;
								}else{
									uni.showToast({
										title: '获取微信信息失败，请稍后再试',
										icon: 'none',
										duration: 3000
									})
								}
							},
							fail(res) {
								console.log(res);
								uni.showToast({
									title: '连接服务器失败，请稍后再试',
									icon: 'none',
									duration: 3000
								})
							}
						});
					},
				});
			},
			onLoad() {//默认加载
				console.log('login');
				this.login();
			}
		}
	}
</script>

<style>
	.btn{
	  width:80%;
	  height:100rpx;
	  background:#0CB9D5;
	  color:#fff;
	  font-size:40rpx;
	  text-align:center;
	  margin:80rpx auto;
	  border-radius:50rpx;
	  overflow:hidden;
	}
	.sign-btn{
		display:flex;
		width:80%;
		height:100rpx;
		background-color:#0CB9D5;
		color:#fff;
		font-size:40rpx;
		border-radius:50rpx;
		justify-content: center;
		align-items:center;
	}
	.signup-box{
	  display:flex;
	  height:300rpx;
	  width:100%;
	  margin:20px auto;
	  flex-flow:column;
	  justify-content: space-around;
	  align-items:center
	}
	.signup-input{
	  display:block;
	  height:80rpx;
	  width:80%;
	  font-size:40rpx;
	  border:1px solid #dedede;
	  border-radius:40rpx;
	  text-indent: 30rpx;
	}
</style>
